package org.example.springbirthday.service;

import lombok.extern.slf4j.Slf4j;
import org.example.springbirthday.mapper.BlessInfoMapper;
import org.example.springbirthday.mapper.NoticeMapper;
import org.example.springbirthday.mapper.UserInfoMapper;
import org.example.springbirthday.model.BlessInfo;
import org.example.springbirthday.model.Notice;
import org.example.springbirthday.model.UserInfo;
import org.example.springbirthday.utils.BirthdayUtil;
import org.example.springbirthday.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 九转苍翎
 * Date: 2025/8/7
 * Time: 02:04
 */
@Slf4j
@Service
public class UserService {
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private BlessInfoMapper blessInfoMapper;
    @Autowired
    private NoticeMapper noticeMapper;

    public UserInfo getUserByName(String userName){
       return userInfoMapper.getUserByName(userName);
    }

    public UserInfo getUserById(Integer id){
        UserInfo userInfo = userInfoMapper.getUserById(id);
        UserService.StatusHandle(userInfo);
        return userInfo;
    }

    public Integer register(String userName, String password) {
        UserInfo userInfo = new UserInfo();
        userInfo.setUserName(userName);
        userInfo.setPassword(password);
        userInfo.setBirthday("2000-01-01");
        userInfo.setPower(0);
        try (InputStream inputStream = getClass().getResourceAsStream("/static/pic/cat.jpg")) {
            if (inputStream != null) {
                userInfo.setFile(inputStream.readAllBytes());
            }
        } catch (Exception e) {
            log.error("cat.jpg资源未找到,e",e);
        }
        //判断身份
        if (userInfo.getPower() == 0){
            userInfo.setPowerStatus("普通用户");
        }else {
            userInfo.setPowerStatus("管理员");
        }
        //格式化时间
        userInfo.setCreateTimeStatus(DateUtils.dateFormat(new Date()));
        userInfo.setUpdateTimeStatus(DateUtils.dateFormat(new Date()));
        return userInfoMapper.register(userInfo);
    }

    public List<UserInfo> getUserInfoListByPageAndSize(Integer offset,Integer size) {
        List<UserInfo> userInfoList = userInfoMapper.getUserInfoListByPageAndSize(offset,size);
        for (UserInfo userInfo : userInfoList) {
            UserService.StatusHandle(userInfo);
        }
        return userInfoList;
    }

    public static void StatusHandle(UserInfo userInfo){
        //判断是否是生日
        boolean birthdayToday = BirthdayUtil.isBirthdayToday(userInfo.getBirthday());
        userInfo.setIsBirthday(birthdayToday);
        if (birthdayToday){
            userInfo.setPowerStatus("寿星");
        }
    }

    public List<UserInfo> isBirthday() {
        List<UserInfo> userInfoList = userInfoMapper.getUserInfoList();
        for (int i = 0;i < userInfoList.size();i++) {
            UserInfo userInfo = userInfoList.get(i);
            //判断是否是生日
            boolean birthdayToday = BirthdayUtil.isBirthdayToday(userInfo.getBirthday());
            userInfo.setIsBirthday(birthdayToday);
            //
            if(birthdayToday) {
                userInfo.setPowerStatus("寿星");
            }else {
                userInfoList.remove(userInfo);
                i--;
            }
        }
        return userInfoList;
    }

    public Integer cancel(Integer userId) {
        //同步删除其发布的祝福
        List<BlessInfo> blessInfoList = blessInfoMapper.getBlessInfoListByUserId(userId);
        try{
            for (BlessInfo blessInfo : blessInfoList) {
                Integer ret = blessInfoMapper.removeById(blessInfo.getId());
                if (ret == 1) {
                    log.info("用户已注销,删除该祝福:blessInfo{}", blessInfo);
                }
            }
        }catch (Exception e){
            log.error("该用户没有祝福语,userId:{},e:",userId,e);
        }
        //同步删除其发布的公告
        List<Notice> noticeList = noticeMapper.getNoticeByUserId(userId);
        try {
            for (Notice notice : noticeList) {
                Integer ret = noticeMapper.delete(notice.getId());
                if (ret == 1) {
                    log.info("用户已注销,删除该公告:notice{}", notice);
                }
            }
        }catch (Exception e){
            log.error("该用户没有公告,userId:{},e:",userId,e);
        }
        //更新祝福语总数
        Integer count = blessInfoMapper.count();
        count -= blessInfoList.size();
        Integer ret = blessInfoMapper.updateCount(count);
        return userInfoMapper.cancel(userId) + ret;
    }

    public Integer update(UserInfo userInfo) {
        userInfo.setUpdateTimeStatus(DateUtils.dateFormat(new Date()));
        //更新其发布的祝福
        List<BlessInfo> blessInfoList = blessInfoMapper.getBlessInfoListByUserId(userInfo.getId());
        try {
            for (BlessInfo blessInfo : blessInfoList) {
                blessInfo.setUserName(userInfo.getUserName());
                try {
                    Integer ret = blessInfoMapper.updateUserName(blessInfo);
                    if (ret != 1) {
                        throw new RuntimeException();
                    }
                } catch (Exception e) {
                    log.error("发布者改名同时更改其发布的祝福语,祝福语改名失败");
                }
            }
        }catch (Exception e){
            log.error("该用户没有祝福语,userInfo:{},e:",userInfo,e);
        }
        //更新其发布的公告
        List<Notice> noticeList = noticeMapper.getNoticeByUserId(userInfo.getId());
        try {
            for (Notice notice : noticeList) {
                notice.setUserName(userInfo.getUserName());
                Integer ret = noticeMapper.updateUserName(notice);
                try {
                    if (ret != 1) {
                        throw new RuntimeException();
                    }
                }catch (Exception e) {
                    log.error("发布者改名同时更改其发布的公告,公告改名失败");
                }
            }
        }catch (Exception e){
            log.error("该用户没有公告,userInfo:{},e:",userInfo,e);
        }
        return userInfoMapper.update(userInfo);
    }

    public Integer save(UserInfo userInfo) {
        return userInfoMapper.save(userInfo);
    }

    public InputStream getFileById(Integer id) {
        return userInfoMapper.getFileById(id);
    }
}
